/*
** Copyright (C) 2010 Calin Radoni
**
** This file is part of CHScanner.
**
** CHScanner is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** CHScanner is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with CHScanner. If not, see <http://www.gnu.org/licenses/>.
*/

#include "StdAfx.h"
#include ".\cfileop.h"

#include <shlwapi.h>

//---------------------------------------------------------------------------

cFileOp::cFileOp(void)
{
}

cFileOp::~cFileOp(void)
{
}

//---------------------------------------------------------------------------

CString cFileOp::GetPathFromExistingFileName(CString fileName)
{
	CString pathName;
	CFileFind finder;
	DWORD err;

	pathName = _T("");

	if(!finder.FindFile(fileName, 0))
		return pathName;

	if(!finder.FindNextFile()){
		err = GetLastError();
		if(err==ERROR_NO_MORE_FILES)
			pathName = finder.GetRoot();
	}

	return pathName;
}

//---------------------------------------------------------------------------

CString cFileOp::EnumFiles(CString folderNameIn, __EnumFileCallback callbackFunc)
{
	CFileFind finder;
	CString fileMask;
	CString pathName;
	CString fileName;
	DWORD err;
	bool done;
	bool goon;

	fileMask  = folderNameIn;
	fileMask += "\\*.p7c";

	pathName = _T("");

	if(!finder.FindFile(fileMask, 0))
		return pathName;

	done = false;
	goon = true;
	while(!done){
		if(!finder.FindNextFile()){
			err = GetLastError();
			if(err!=ERROR_NO_MORE_FILES)
				goon = false;
			done = true;
		}

		if(goon){
			pathName = finder.GetRoot();
			fileName = finder.GetFileName();
			if(callbackFunc!=NULL)
				callbackFunc((TCHAR*)(LPCTSTR)fileName);
		}
	}

	return pathName;
}

//---------------------------------------------------------------------------

bool cFileOp::IsInvalidCharInFileName(CString strIn)
{
	int pos;

	pos = strIn.Find('\\');
	if(pos!=-1) return true;
	pos = strIn.Find('/');
	if(pos!=-1) return true;
	pos = strIn.Find(':');
	if(pos!=-1) return true;
	pos = strIn.Find('*');
	if(pos!=-1) return true;
	pos = strIn.Find('?');
	if(pos!=-1) return true;
	pos = strIn.Find('"');
	if(pos!=-1) return true;
	pos = strIn.Find('<');
	if(pos!=-1) return true;
	pos = strIn.Find('>');
	if(pos!=-1) return true;
	pos = strIn.Find('|');
	if(pos!=-1) return true;

	return false;
}

//---------------------------------------------------------------------------

bool cFileOp::FileExists(CString strIn)
{
	BOOL bRet;

	bRet = PathFileExists(strIn);

	return bRet ? true : false;
}

//---------------------------------------------------------------------------
